﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class guestbook : System.Web.UI.Page
{
    Message m = new Message();
    int sumPage = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            Bind();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request["checkcode"].ToString().ToUpper() != Session["User"].ToString().ToUpper())
        {
            Response.Write("<script>alert('验证码错误');history.go(-1);</script>");
            Response.End();
        }
        else
        {
            m.Face = Request["Radio1"].ToString();
            if (Request["username"].ToString()!="")
                m.Name = Request["username"].ToString();
            else
                m.Name = "三木访客";
            m.Email = Request["email"].ToString();
            if (Request["title"].ToString() != "")
                m.Title = Request["title"].ToString();
            else
                m.Title = "无主题";
            m.Content = Request["content"].ToString();
            m.Addtime = DateTime.Now;
            m.SendMsg(m);
            Response.Write("<script>alert('发表成功,等待管理员审核...');location.href('guestbook.aspx');</script>");
        }
    }

    private void Bind()
    {
        int pageNo = 1;
        int pageSize = 3;
        if (Request.QueryString["page"] == null)
        {
            pageNo = 1;
        }
        else
        {
            pageNo = Int32.Parse(Request.QueryString["page"]);
        }


        SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand();
        da.SelectCommand.Connection = conn;
        da.SelectCommand.CommandText = "getdataset";
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "*";
        da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "message";
        da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = " replycontent <>'' ";
        da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "mid";
        da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by mid desc";
        da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
        da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
        da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
        da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
        DataSet ds = new DataSet();
        da.Fill(ds, "guestbook");
        this.DataList1.DataSource = ds;
        this.DataList1.DataBind();

        Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value;
        Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value;
        lbRecord.Text = RecordCount.ToString();
        lbRow.Text = pageNo.ToString();
        sumPage = (Int32)RecordCount / pageSize;

        if (RecordCount % pageSize > 0)
        {
            sumPage = sumPage + 1;
        }
        lbpage.Text = sumPage.ToString();
        if (pageNo > 1)
        {
            hylfirst.NavigateUrl = "?page=1";
            hylprev.NavigateUrl = string.Concat("?page=", "", pageNo - 1);
        }
        else
        {
            hylprev.NavigateUrl = "";
            hylfirst.NavigateUrl = "";
            hylfirst.Enabled = false;
            hylprev.Enabled = false;
        }
        if (pageNo < sumPage)
        {
            hylend.NavigateUrl = string.Concat("?page=", "", sumPage);
            hylnext.NavigateUrl = string.Concat("?page=", "", pageNo + 1);
        }
        else
        {
            hylnext.NavigateUrl = "";
            hylend.NavigateUrl = "";
            hylend.Enabled = false;
            hylnext.Enabled = false;
        }
    }
}
